/*   
 * Copyright 2009-2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.beaconframework.vaadin.ui;

import org.beaconframework.vaadin.util.VaadinContext;
import org.springframework.security.core.userdetails.UserDetails;

import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.Panel;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class ApplicationLayoutWindow extends Window {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    Panel headerPanel;
    SplitPanel horizontalSplitPanel;
    Panel leftPanel;
    TabSheet mainTabSheet;

    VerticalLayout mainVerticalLayout;
    Component topComponent = new Label("");
    Label userNameLabel;

    public ApplicationLayoutWindow() {

        // Our main layout is a horizontal layout
        mainVerticalLayout = new VerticalLayout();
        mainVerticalLayout.setSizeFull();
        setContent(mainVerticalLayout);

        headerPanel = createHeaderPanel();

        addComponent(headerPanel);

        horizontalSplitPanel = new SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL);
        horizontalSplitPanel.setWidth("100%");
        horizontalSplitPanel.setSplitPosition(220, Sizeable.UNITS_PIXELS);

        horizontalSplitPanel.setHeight("100%");
        addComponent(horizontalSplitPanel);
        mainVerticalLayout.setExpandRatio(horizontalSplitPanel, 1.0f);

        leftPanel = new Panel();
        leftPanel.setScrollable(false);
        horizontalSplitPanel.addComponent(leftPanel);
        horizontalSplitPanel.addComponent(new Label(""));
        leftPanel.setHeight("100%");

        //

        // setHeaderComponent(new Label("Header"));

        mainTabSheet = new TabSheet();
        // mainTabSheet.addTab(new Label("Content"),"Tab",null);
        mainTabSheet.hideTabs(true);
        setMainComponent(mainTabSheet);

    }

    @Override
    public void attach() {

        super.attach();

        Object user = getApplication().getUser();
        if (user == null) {
            user = "unknown";
        } else if (user instanceof org.springframework.security.core.userdetails.UserDetails) {
            userNameLabel.setValue(((UserDetails) user).getUsername());
        } else {
            userNameLabel.setValue(user);
        }
    }

    public Panel createHeaderPanel() {
        Panel headerPanel = new Panel();
        HorizontalLayout hl = new HorizontalLayout();
        headerPanel.setContent(hl);
        headerPanel.setScrollable(false);
        headerPanel.setWidth("100%");
        hl.setWidth("100%");
        headerPanel.setHeight("75px");
        Embedded e = new Embedded("", new ClassResource("/hhg.png",
                VaadinContext.getApplication()));
        hl.addComponent(e);
        userNameLabel = new Label("");

        Link link = new Link("Logout", new ExternalResource(
                "j_spring_security_logout"));
        hl.addComponent(userNameLabel);
        hl.addComponent(link);
        return headerPanel;
    }

    public TabSheet getMainTabSheet() {
        return mainTabSheet;
    }

    public Component getLeftComponent() {
        Panel p = (Panel) horizontalSplitPanel.getFirstComponent();
        return p.getComponentIterator().next();
    }

    public void setLeftComponent(Component c) {
        Panel old = (Panel) horizontalSplitPanel.getFirstComponent();
        old.removeAllComponents();
        old.addComponent(c);
    }

    public void setMainComponent(Component c) {
        Component old = horizontalSplitPanel.getSecondComponent();
        horizontalSplitPanel.replaceComponent(old, c);
        horizontalSplitPanel.setMargin(false);

    }

}
